// ----------------------------------------------------------------------
// Include files
// ----------------------------------------------------------------------
#include	<string.h>
#include 	<fcntl.h>
#include 	<stdio.h>
#include 	<unistd.h>
#include 	<stdlib.h>
#include 	<sys/ioctl.h>

#include	"RN6264_i2c.h"

static u8 RN_6264_addr = 0x2c;


int RN6264_I2c_write(u8 reg,u8 val)
{
	char *i2c_dev;
	u8 ret;
	u8 w8[2];
	w8[0]=reg;
	w8[1]=val;
	i2c_dev="/dev/i2c-0";
	int i2c_fd;
	i2c_fd = open(i2c_dev, O_RDWR, 0);
	if (i2c_fd < 0)
	{
		printf("can't open %s \n",i2c_dev);
		return -1;
	}
	ret = ioctl(i2c_fd, I2C_TENBIT, 0);
	if (ret < 0)
		return -1;
	ret = ioctl(i2c_fd, I2C_SLAVE, RN_6264_addr);
	if (ret < 0)
		return -1;
	ret = write(i2c_fd, &w8, 2);
//	printf ("reg[0x%02x] value[0x%02x]\n",w8[0],w8[1]);
	if (ret < 1)
		return -1;
	close(i2c_fd);
	return 0;
}

int RN6264_I2c_read(u8 reg,u8 *val)
{
	char *i2c_dev;
	u8 ret;

	i2c_dev="/dev/i2c-0";
	int i2c_fd;
	i2c_fd = open(i2c_dev, O_RDWR, 0);
	if (i2c_fd < 0)
	{
		printf("can't open %s \n",i2c_dev);
		return -1;
	}
	ret = ioctl(i2c_fd, I2C_TENBIT, 0);
	if (ret < 0)
		return -1;
	ret = ioctl(i2c_fd, I2C_SLAVE, RN_6264_addr);
	if (ret < 0)
		return -1;
	ret = write(i2c_fd, &reg, 1);
	ret = read(i2c_fd, val, 1);
//	printf ("reg[0x%02x] value[0x%02x]\n",reg,*val);
	if (ret < 1)
		return -1;
	close(i2c_fd);
	return 0;
}


int RN6264_RegWrite(u8 reg,u8 val)
{
	if ( RN6264_I2c_write(reg, val) < 0)
		return -1;
	else
		return 0;
}

// ----------------------------------------------------------------------
// Static Global Data section variables
// ----------------------------------------------------------------------

static const u8 defRN6266Sys_PAL[] = {

// Added
//---------------------------------------------------------
	0x80,0x31,  //Enable Global reset
	0x81,0x0f,  //Video CH0/1/2/3 enable, Audio DAC/ADC disable, Video DAC disable

//---------------------------------------------------------
//	AGC logic sel 11 10, 01 00,=> E4
	0xDE, 0xE4,

//---------------------------------------------------------
//	BT.656 Clock Setting
//	CLK_Freq		CLK_Enable
	0x83, 0x01,	0x82, 0x01,	// BT.656 Clock0 Configuration,54MHz output, Enable CLK0
	0x85, 0x01,	0x84, 0x03,	// BT.656 Clock1 Configuration,54MHz output, Enable CLK1,Invert
	0x87, 0x00,	0x86, 0x00,	// BT.656 Clock2 Configuration
	0x89, 0x00,	0x88, 0x00,	// BT.656 Clock3 Configuration

//---------------------------------------------------------
//	BT.656 Data Port Setting
//	Port EN,	Input Sel,	Port Dly,
	0x8a, 0x01,	0x8b, 0x31,	0x8c, 0x00,   0x8a, 0x01,    // BT.656 Data Port0 Configuration, 27Mhz data rate, Enable Data port1,DDR/0/1CH
	0x8d, 0x01,	0x8e, 0xc1,	0x8f, 0x00,   0x8d, 0x01,    // BT.656 Data Port1 Configuration, 27Mhz data rate, Enable Data port3,DDR/2/3CH
	0x90, 0x00,	0x91, 0x00,	0x92, 0x00,   0x90, 0x00,    // BT.656 Data Port2 Configuration
	0x93, 0x00,	0x94, 0x00,	0x95, 0x00,   0x93, 0x00,    // BT.656 Data Port3 Configuration


//---------------------------------------------------------
//	Video Input Setting

//	Page,		Mode Set,	525/625 Line,
	0xff,0x00,	0x00,0x00,	0x07,0x22,    // Standard video support, E 2D/5H comb,Forces operation in 625-Line
	0xff,0x01,	0x00,0x00,	0x07,0x22,    // Standard video support, E 2D/5H comb,Forces operation in 625-Line
	0xff,0x02,	0x00,0x00,	0x07,0x22,    // Standard video support, E 2D/5H comb,Forces operation in 625-Line
	0xff,0x03,	0x00,0x00,	0x07,0x22,    // Standard video support, E 2D/5H comb,Forces operation in 625-Line
//	Page,		Mode Set,	525/625 Line,
//	0xff,0x00,	0x00,0x10,	0x07,0x23,    // Standard video support, E 2D/5H comb,Forces operation in 525-Line
//	0xff,0x01,	0x00,0x10,	0x07,0x23,    // Standard video support, E 2D/5H comb,Forces operation in 525-Line
//	0xff,0x02,	0x00,0x10,	0x07,0x23,    // Standard video support, E 2D/5H comb,Forces operation in 525-Line
//	0xff,0x03,	0x00,0x10,	0x07,0x23,    // Standard video support, E 2D/5H comb,Forces operation in 525-Line
//	Page,		Mode Set,	525/625 Line,
//	0xff,0x00,	0x00,0x00,	0x07,0x20,    // Standard video support, E 2D/5H comb,operation automatically in NTSC/PAL
//	0xff,0x01,	0x00,0x00,	0x07,0x20,    // Standard video support, E 2D/5H comb,operation automatically in NTSC/PAL
//	0xff,0x02,	0x00,0x00,	0x07,0x20,    // Standard video support, E 2D/5H comb,operation automatically in NTSC/PAL
//	0xff,0x03,	0x00,0x00,	0x07,0x20,    // Standard video support, E 2D/5H comb,operation automatically in NTSC/PAL
//	Test pattern Generator
//	Page,		Mode Set,
//	0xff,0x00,	0x00,0xc0,// Test pattern(100% Bars) output, E 2D/5H comb
//	0xff,0x01,	0x00,0xc0,// Test pattern(100% Bars) output, E 2D/5H comb
//	0xff,0x02,	0x00,0xc0,// Test pattern(100% Bars) output, E 2D/5H comb
//	0xff,0x03,	0x00,0xc0,// Test pattern(100% Bars) output, E 2D/5H comb

//---------------------------------------------------------
//	Basic calibration
//	Page,		Bright,		Contrast,	Saturation,	Hue,		Sharpness,
	0xFF, 0x00,	0x01, 0xE8,	0x02,0x88,	0x03,0x89,	0x04,0x89,	0x05,0x08,
	0xFF, 0x01,	0x01, 0xE8,	0x02,0x88,	0x03,0x89,	0x04,0x89,	0x05,0x08,
	0xFF, 0x02,	0x01, 0xE8,	0x02,0x88,	0x03,0x89,	0x04,0x89,	0x05,0x08,
	0xFF, 0x03,	0x01, 0xE8,	0x02,0x88,	0x03,0x89,	0x04,0x89,	0x05,0x08,

//---------------------------------------------------------
//	Cropping Setting ->default
//	Page,	E/D,	H/V S/E-line[9:8],	H S-line[7:0],	H E-line[7:0],	V0 S-line[7:0],	V0 E-line[7:0],	V1 S-line[7:0],	V1 E-line[7:0],
	0xFF, 0x00,	0x28, 0x82,	0x20, 0xa4,	0x21, 0x42,	0x22, 0xAC,	0x23, 0x16,	0x24, 0x05,	0x25, 0x16,	0x26, 0x05,	0x27, 0x68,
	0xFF, 0x01,	0x28, 0x82,	0x20, 0xa4,	0x21, 0x42,	0x22, 0xAC,	0x23, 0x16,	0x24, 0x05,	0x25, 0x16,	0x26, 0x05,	0x27, 0x68,
	0xFF, 0x02,	0x28, 0x82,	0x20, 0xa4,	0x21, 0x42,	0x22, 0xAC,	0x23, 0x16,	0x24, 0x05,	0x25, 0x16,	0x26, 0x05,	0x27, 0x68,
	0xFF, 0x03,	0x28, 0x82,	0x20, 0xa4,	0x21, 0x42,	0x22, 0xAC,	0x23, 0x16,	0x24, 0x05,	0x25, 0x16,	0x26, 0x05,	0x27, 0x68,

//---------------------------------------------------------
//	Downscale Setting ->default
//	Page,		Downscal,
	0xFF, 0x00,	0x1F, 0x20,	// V scaling(1:1),H scaling(1:1)
	0xFF, 0x01,	0x1F, 0x20,	// V scaling(1:1),H scaling(1:1)
	0xFF, 0x02,	0x1F, 0x20,	// V scaling(1:1),H scaling(1:1)
	0xFF, 0x03,	0x1F, 0x20,	// V scaling(1:1),H scaling(1:1)

//---------------------------------------------------------
//	Downscale Cropping Setting ->default
//	Page,	H/V S/E-line[9:8],	V0 S-line[7:0],	V0 E-line[7:0],	V1 S-line[7:0],	V1 E-line[7:0],
	0xFF, 0x00,	0x40, 0xA2,	0x41, 0x16,	0x42, 0x05,	0x43, 0x16,	0x44, 0x05,	0x45, 0x68,
	0xFF, 0x01,	0x40, 0xA2,	0x41, 0x16,	0x42, 0x05,	0x43, 0x16,	0x44, 0x05,	0x45, 0x68,
	0xFF, 0x02,	0x40, 0xA2,	0x41, 0x16,	0x42, 0x05,	0x43, 0x16,	0x44, 0x05,	0x45, 0x68,
	0xFF, 0x03,	0x40, 0xA2,	0x41, 0x16,	0x42, 0x05,	0x43, 0x16,	0x44, 0x05,	0x45, 0x68,

//---------------------------------------------------------
//	Advance Setting
//	Page,      Chrome,Blus Scree E/D, D-AGC-L,   D-AGC-B,   Notch control,  Bessel,    SYNC_sA-AGC, Blank_R,
	0xFF, 0x00,	0x06, 0x08,	0x09, 0x80,	0x0A, 0xC0,	0x0B, 0x80,	0x0C, 0x80,	0x0E, 0xB0,	0x11,0x03, 0x1A, 0x83,	0x1c, 0xE6,0x1d, 0xCF,0x1E, 0xF0,	0x2B, 0x49,	0x2c, 0xb0,	0x32, 0xd0,0x36, 0x20,0x38, 0x10,
	0xFF, 0x01,	0x06, 0x08,	0x09, 0x80,	0x0A, 0xC0,	0x0B, 0x80,	0x0C, 0x80,	0x0E, 0xB0,	0x11,0x03, 0x1A, 0x83,	0x1c, 0xE6,0x1d, 0xCF,0x1E, 0xF0,	0x2B, 0x49,	0x2c, 0xb0,	0x32, 0xd0,0x36, 0x20,0x38, 0x10,
	0xFF, 0x02,	0x06, 0x08,	0x09, 0x80,	0x0A, 0xC0,	0x0B, 0x80,	0x0C, 0x80,	0x0E, 0xB0,	0x11,0x03, 0x1A, 0x83,	0x1c, 0xE6,0x1d, 0xCF,0x1E, 0xF0,	0x2B, 0x49,	0x2c, 0xb0,	0x32, 0xd0,0x36, 0x20,0x38, 0x10,
	0xFF, 0x03,	0x06, 0x08,	0x09, 0x80,	0x0A, 0xC0,	0x0B, 0x80,	0x0C, 0x80,	0x0E, 0xB0,	0x11,0x03, 0x1A, 0x83,	0x1c, 0xE6,0x1d, 0xCF,0x1E, 0xF0,	0x2B, 0x49,	0x2c, 0xb0,	0x32, 0xd0,0x36, 0x20,0x38, 0x10,

//---------------------------------------------------------
//	ID Insertion Setting
//	Page, Chid format,  Chid,
	0xFF, 0x00,	0x3A, 0x20,	0x3E, 0xFF,	0x3F, 0x10,	// Insert Channel ID in SAV/EAV, Channel ID "MSB of XY=1"
	0xFF, 0x01,	0x3A, 0x20,	0x3E, 0xFF,	0x3F, 0x11,	// Insert Channel ID in SAV/EAV, Channel ID "MSB of XY=1"
	0xFF, 0x02,	0x3A, 0x20,	0x3E, 0xFF,	0x3F, 0x12,	// Insert Channel ID in SAV/EAV, Channel ID "MSB of XY=1"
	0xFF, 0x03,	0x3A, 0x20,	0x3E, 0xFF,	0x3F, 0x13,	// Insert Channel ID in SAV/EAV, Channel ID "MSB of XY=1"
};



// ----------------------------------------------------------------------
// Static Global Data section variables
// ----------------------------------------------------------------------
static const u8 defRN6266Sys_NTSC[] = {

// Added
//---------------------------------------------------------
	0x80,0x31,  //Enable Global reset
	0x81,0x0f,  //Video CH0/1/2/3 enable, Audio DAC/ADC disable, Video DAC disable

//---------------------------------------------------------
//	AGC logic sel 11 10, 01 00,=> E4
	0xDE, 0xE4,

//---------------------------------------------------------
//	BT.656 Clock Setting
//	CLK_Freq		CLK_Enable
	0x83, 0x01,	0x82, 0x01,	// BT.656 Clock0 Configuration,54MHz output, Enable CLK0
	0x85, 0x01,	0x84, 0x03,	// BT.656 Clock1 Configuration,54MHz output, Enable CLK1,Invert
	0x87, 0x00,	0x86, 0x00,	// BT.656 Clock2 Configuration
	0x89, 0x00,	0x88, 0x00,	// BT.656 Clock3 Configuration

//---------------------------------------------------------
//	BT.656 Data Port Setting
//	Port EN,	Input Sel,	Port Dly,
	0x8a, 0x01,	0x8b, 0x31,	0x8c, 0x00,   0x8a, 0x01,    // BT.656 Data Port0 Configuration, 27Mhz data rate, Enable Data port1,DDR/0/1CH
	0x8d, 0x01,	0x8e, 0xc1,	0x8f, 0x00,   0x8d, 0x01,    // BT.656 Data Port1 Configuration, 27Mhz data rate, Enable Data port3,DDR/2/3CH
	0x90, 0x00,	0x91, 0x00,	0x92, 0x00,   0x90, 0x00,    // BT.656 Data Port2 Configuration
	0x93, 0x00,	0x94, 0x00,	0x95, 0x00,   0x93, 0x00,    // BT.656 Data Port3 Configuration


//---------------------------------------------------------
//	Video Input Setting

//	Page,		Mode Set,	525/625 Line,
//	0xff,0x00,	0x00,0x00,	0x07,0x22,    // Standard video support, E 2D/5H comb,Forces operation in 625-Line
//	0xff,0x01,	0x00,0x00,	0x07,0x22,    // Standard video support, E 2D/5H comb,Forces operation in 625-Line
//	0xff,0x02,	0x00,0x00,	0x07,0x22,    // Standard video support, E 2D/5H comb,Forces operation in 625-Line
//	0xff,0x03,	0x00,0x00,	0x07,0x22,    // Standard video support, E 2D/5H comb,Forces operation in 625-Line
//	Page,		Mode Set,	525/625 Line,
	0xff,0x00,	0x00,0x10,	0x07,0x23,    // Standard video support, E 2D/5H comb,Forces operation in 525-Line
	0xff,0x01,	0x00,0x10,	0x07,0x23,    // Standard video support, E 2D/5H comb,Forces operation in 525-Line
	0xff,0x02,	0x00,0x10,	0x07,0x23,    // Standard video support, E 2D/5H comb,Forces operation in 525-Line
	0xff,0x03,	0x00,0x10,	0x07,0x23,    // Standard video support, E 2D/5H comb,Forces operation in 525-Line
//	Page,		Mode Set,	525/625 Line,
//	0xff,0x00,	0x00,0x00,	0x07,0x20,    // Standard video support, E 2D/5H comb,operation automatically in NTSC/PAL
//	0xff,0x01,	0x00,0x00,	0x07,0x20,    // Standard video support, E 2D/5H comb,operation automatically in NTSC/PAL
//	0xff,0x02,	0x00,0x00,	0x07,0x20,    // Standard video support, E 2D/5H comb,operation automatically in NTSC/PAL
//	0xff,0x03,	0x00,0x00,	0x07,0x20,    // Standard video support, E 2D/5H comb,operation automatically in NTSC/PAL
//	Test pattern Generator
//	Page,		Mode Set,
//	0xff,0x00,	0x00,0xc0,// Test pattern(100% Bars) output, E 2D/5H comb
//	0xff,0x01,	0x00,0xc0,// Test pattern(100% Bars) output, E 2D/5H comb
//	0xff,0x02,	0x00,0xc0,// Test pattern(100% Bars) output, E 2D/5H comb
//	0xff,0x03,	0x00,0xc0,// Test pattern(100% Bars) output, E 2D/5H comb

//---------------------------------------------------------
//	Basic calibration
//	Page,		Bright,		Contrast,	Saturation,	Hue,		Sharpness,
	0xFF, 0x00,	0x01, 0xE8,	0x02,0x88,	0x03,0x89,	0x04,0x89,	0x05,0x08,
	0xFF, 0x01,	0x01, 0xE8,	0x02,0x88,	0x03,0x89,	0x04,0x89,	0x05,0x08,
	0xFF, 0x02,	0x01, 0xE8,	0x02,0x88,	0x03,0x89,	0x04,0x89,	0x05,0x08,
	0xFF, 0x03,	0x01, 0xE8,	0x02,0x88,	0x03,0x89,	0x04,0x89,	0x05,0x08,

//---------------------------------------------------------
//	Cropping Setting ->default
//	Page,	E/D,	H/V S/E-line[9:8],	H S-line[7:0],	H E-line[7:0],	V0 S-line[7:0],	V0 E-line[7:0],	V1 S-line[7:0],	V1 E-line[7:0],
	0xFF, 0x00,	0x28, 0x82,	0x20, 0xa4,	0x21, 0x42,	0x22, 0xAC,	0x23, 0x16,	0x24, 0x05,	0x25, 0x16,	0x26, 0x05,	0x27, 0x68,
	0xFF, 0x01,	0x28, 0x82,	0x20, 0xa4,	0x21, 0x42,	0x22, 0xAC,	0x23, 0x16,	0x24, 0x05,	0x25, 0x16,	0x26, 0x05,	0x27, 0x68,
	0xFF, 0x02,	0x28, 0x82,	0x20, 0xa4,	0x21, 0x42,	0x22, 0xAC,	0x23, 0x16,	0x24, 0x05,	0x25, 0x16,	0x26, 0x05,	0x27, 0x68,
	0xFF, 0x03,	0x28, 0x82,	0x20, 0xa4,	0x21, 0x42,	0x22, 0xAC,	0x23, 0x16,	0x24, 0x05,	0x25, 0x16,	0x26, 0x05,	0x27, 0x68,

//---------------------------------------------------------
//	Downscale Setting ->default
//	Page,		Downscal,
	0xFF, 0x00,	0x1F, 0x20,	// V scaling(1:1),H scaling(1:1)
	0xFF, 0x01,	0x1F, 0x20,	// V scaling(1:1),H scaling(1:1)
	0xFF, 0x02,	0x1F, 0x20,	// V scaling(1:1),H scaling(1:1)
	0xFF, 0x03,	0x1F, 0x20,	// V scaling(1:1),H scaling(1:1)

//---------------------------------------------------------
//	Downscale Cropping Setting ->default
//	Page,	H/V S/E-line[9:8],	V0 S-line[7:0],	V0 E-line[7:0],	V1 S-line[7:0],	V1 E-line[7:0],
	0xFF, 0x00,	0x40, 0xA2,	0x41, 0x16,	0x42, 0x05,	0x43, 0x16,	0x44, 0x05,	0x45, 0x68,
	0xFF, 0x01,	0x40, 0xA2,	0x41, 0x16,	0x42, 0x05,	0x43, 0x16,	0x44, 0x05,	0x45, 0x68,
	0xFF, 0x02,	0x40, 0xA2,	0x41, 0x16,	0x42, 0x05,	0x43, 0x16,	0x44, 0x05,	0x45, 0x68,
	0xFF, 0x03,	0x40, 0xA2,	0x41, 0x16,	0x42, 0x05,	0x43, 0x16,	0x44, 0x05,	0x45, 0x68,

//---------------------------------------------------------
//	Advance Setting
//	Page,      Chrome,Blus Scree E/D, D-AGC-L,   D-AGC-B,   Notch control,  Bessel,    SYNC_sA-AGC, Blank_R,
	0xFF, 0x00,	0x06, 0x08,	0x09, 0x80,	0x0A, 0xC0,	0x0B, 0x80,	0x0C, 0x80,	0x0E, 0xB0,	0x11,0x03, 0x1A, 0x83,	0x1c, 0xE6,0x1d, 0xCF,0x1E, 0xF0,	0x2B, 0x49,	0x2c, 0xb0,	0x32, 0xd0,0x36, 0x20,0x38, 0x10,
	0xFF, 0x01,	0x06, 0x08,	0x09, 0x80,	0x0A, 0xC0,	0x0B, 0x80,	0x0C, 0x80,	0x0E, 0xB0,	0x11,0x03, 0x1A, 0x83,	0x1c, 0xE6,0x1d, 0xCF,0x1E, 0xF0,	0x2B, 0x49,	0x2c, 0xb0,	0x32, 0xd0,0x36, 0x20,0x38, 0x10,
	0xFF, 0x02,	0x06, 0x08,	0x09, 0x80,	0x0A, 0xC0,	0x0B, 0x80,	0x0C, 0x80,	0x0E, 0xB0,	0x11,0x03, 0x1A, 0x83,	0x1c, 0xE6,0x1d, 0xCF,0x1E, 0xF0,	0x2B, 0x49,	0x2c, 0xb0,	0x32, 0xd0,0x36, 0x20,0x38, 0x10,
	0xFF, 0x03,	0x06, 0x08,	0x09, 0x80,	0x0A, 0xC0,	0x0B, 0x80,	0x0C, 0x80,	0x0E, 0xB0,	0x11,0x03, 0x1A, 0x83,	0x1c, 0xE6,0x1d, 0xCF,0x1E, 0xF0,	0x2B, 0x49,	0x2c, 0xb0,	0x32, 0xd0,0x36, 0x20,0x38, 0x10,

//---------------------------------------------------------
//	ID Insertion Setting
//	Page, Chid format,  Chid,
	0xFF, 0x00,	0x3A, 0x20,	0x3E, 0xFF,	0x3F, 0x10,	// Insert Channel ID in SAV/EAV, Channel ID "MSB of XY=1"
	0xFF, 0x01,	0x3A, 0x20,	0x3E, 0xFF,	0x3F, 0x11,	// Insert Channel ID in SAV/EAV, Channel ID "MSB of XY=1"
	0xFF, 0x02,	0x3A, 0x20,	0x3E, 0xFF,	0x3F, 0x12,	// Insert Channel ID in SAV/EAV, Channel ID "MSB of XY=1"
	0xFF, 0x03,	0x3A, 0x20,	0x3E, 0xFF,	0x3F, 0x13,	// Insert Channel ID in SAV/EAV, Channel ID "MSB of XY=1"
};

static const u8 defRN6266Sys_NTSC_960[] = {

// Added
//---------------------------------------------------------
	0x80,0x31,  //Enable Global reset
	0x81,0x0f,  //Video CH0/1/2/3 enable, Audio DAC/ADC disable, Video DAC disable

//---------------------------------------------------------
//	AGC logic sel 11 10, 01 00,=> E4
	0xDE, 0xE4,

//---------------------------------------------------------
//	BT.656 Clock Setting
//	CLK_Freq,    CLK_Enable,
        0x83,0x09,   0x82,0x81,   // Pin34,  SCLK0A, BT.656 Clock0A Configuration,36MHz output, Enable
        0x85,0x09,   0x84,0x83,   // Pin35,  SCLK0B, BT.656 Clock0B Configuration,36MHz output, Enable, Invert


//---------------------------------------------------------
//	BT.656 Data Port Setting
//	Port EN,		Input Sel,	Port Dly,
/*	0x8a, 0x10,	0x8b, 0x00,	0x8c, 0x00,   // BT.656 Data Port0 Configuration
	0x8d, 0x10,	0x8e, 0x31,	0x8f, 0x00,   // BT.656 Data Port1 Configuration, 27Mhz data rate, Enable Data port1,DDR/0/1CH
	0x90, 0x10,	0x91, 0x00,	0x92, 0x00,   // BT.656 Data Port2 Configuration
	0x93, 0x10,	0x94, 0xC1,	0x95, 0x00,   // BT.656 Data Port3 Configuration, 27Mhz data rate, Enable Data port3,DDR/2/3CH
*/
// Modified
//	BT.656 Data Port Setting
//	Port EN,	Input Sel,	Port Dly,
	0x8a, 0x31,	0x8b, 0x31,	0x8c, 0x80,   0x8a, 0x31,    // BT.656 Data Port0 Configuration, 36Mhz data rate, Enable Data port1,DDR/0/1CH
	0x8d, 0x01,	0x8e, 0xC1,	0x8f, 0x80,   0x8d, 0x01,    // BT.656 Data Port1 Configuration, 36Mhz data rate, Enable Data port3,DDR/2/3CH
	0x90, 0x00,	0x91, 0x00,	0x92, 0x00,   0x90, 0x00,    // BT.656 Data Port2 Configuration
	0x93, 0x00,	0x94, 0x00,	0x95, 0x00,   0x93, 0x00,    // BT.656 Data Port3 Configuration


//---------------------------------------------------------
//	Video Input Setting

//	Page,		Mode Set,	525/625 Line,
//	0xff,0x00,	0x00,0x00,	0x07,0x22,         // Standard video support, E 2D/5H comb,Forces operation in 625-Line
//	0xff,0x01,	0x00,0x00,	0x07,0x22,         // Standard video support, E 2D/5H comb,Forces operation in 625-Line
//	0xff,0x02,	0x00,0x00,	0x07,0x22,         // Standard video support, E 2D/5H comb,Forces operation in 625-Line
//	0xff,0x03,	0x00,0x00,	0x07,0x22,         // Standard video support, E 2D/5H comb,Forces operation in 625-Line
//	Page,		Mode Set,	525/625 Line,
//	0xff,0x00,	0x00,0x10,	0x07,0x23,         // Standard video support, E 2D/5H comb,Forces operation in 525-Line
//	0xff,0x01,	0x00,0x10,	0x07,0x23,         // Standard video support, E 2D/5H comb,Forces operation in 525-Line
//	0xff,0x02,	0x00,0x10,	0x07,0x23,         // Standard video support, E 2D/5H comb,Forces operation in 525-Line
//	0xff,0x03,	0x00,0x10,	0x07,0x23,         // Standard video support, E 2D/5H comb,Forces operation in 525-Line
//	Page,		Mode Set,	525/625 Line,
	0xff,0x00,	0x00,0x10,	0x07,0x23,    // Standard video support, E 2D/5H comb,Forces operation in 525-Line
	0xff,0x01,	0x00,0x10,	0x07,0x23,    // Standard video support, E 2D/5H comb,Forces operation in 525-Line
	0xff,0x02,	0x00,0x10,	0x07,0x23,    // Standard video support, E 2D/5H comb,Forces operation in 525-Line
	0xff,0x03,	0x00,0x10,	0x07,0x23,    // Standard video support, E 2D/5H comb,Forces operation in 525-Line	Test pattern Generator
//	Page,		Mode Set,
//	0xff,0x00,	0x00,0xc0,          // Test pattern(100% Bars) output, E 2D/5H comb
//	0xff,0x01,	0x00,0xc0,          // Test pattern(100% Bars) output, E 2D/5H comb
//	0xff,0x02,	0x00,0xc0,          // Test pattern(100% Bars) output, E 2D/5H comb
//	0xff,0x03,	0x00,0xc0,          // Test pattern(100% Bars) output, E 2D/5H comb

        0xff,0x00,      0x50,0x01,           // enable 960H mode
        0xff,0x01,      0x50,0x01,           // enable 960H mode
        0xff,0x02,      0x50,0x01,           // enable 960H mode
        0xff,0x03,      0x50,0x01,           // enable 960H mode
//      0xff,0x00,      0x50,0x00           // enable D1 mode
//      0xff,0x01,      0x50,0x00           // enable D1 mode
//      0xff,0x02,      0x50,0x00           // enable D1 mode
//      0xff,0x03,      0x50,0x00           // enable D1 mode



//---------------------------------------------------------
//	Basic calibration
//	Page,		Bright,		Contrast,	Saturation,	Hue,		Sharpness,
	0xFF, 0x00,	0x01, 0xE8,	0x02,0x88,	0x03,0x89,	0x04,0x89,	0x05,0x08,
	0xFF, 0x01,	0x01, 0xE8,	0x02,0x88,	0x03,0x89,	0x04,0x89,	0x05,0x08,
	0xFF, 0x02,	0x01, 0xE8,	0x02,0x88,	0x03,0x89,	0x04,0x89,	0x05,0x08,
	0xFF, 0x03,	0x01, 0xE8,	0x02,0x88,	0x03,0x89,	0x04,0x89,	0x05,0x08,

//---------------------------------------------------------
//	Cropping Setting ->default
//	Page,	E/D,	H/V S/E-line[9:8],	H S-line[7:0],	H E-line[7:0],	V0 S-line[7:0],	V0 E-line[7:0],	V1 S-line[7:0],	V1 E-line[7:0],
	0xFF, 0x00,	0x28, 0x82,	0x20, 0xa4,	0x21, 0x42,	0x22, 0xAC,	0x23, 0x16,	0x24, 0x05,	0x25, 0x16,	0x26, 0x05,	0x27, 0x68,
	0xFF, 0x01,	0x28, 0x82,	0x20, 0xa4,	0x21, 0x42,	0x22, 0xAC,	0x23, 0x16,	0x24, 0x05,	0x25, 0x16,	0x26, 0x05,	0x27, 0x68,
	0xFF, 0x02,	0x28, 0x82,	0x20, 0xa4,	0x21, 0x42,	0x22, 0xAC,	0x23, 0x16,	0x24, 0x05,	0x25, 0x16,	0x26, 0x05,	0x27, 0x68,
	0xFF, 0x03,	0x28, 0x82,	0x20, 0xa4,	0x21, 0x42,	0x22, 0xAC,	0x23, 0x16,	0x24, 0x05,	0x25, 0x16,	0x26, 0x05,	0x27, 0x68,

//---------------------------------------------------------
//	Downscale Setting ->default
//	Page,		Downscal,
	0xFF, 0x00,	0x1F, 0x20,	// V scaling(1:1),H scaling(1:1)
	0xFF, 0x01,	0x1F, 0x20,	// V scaling(1:1),H scaling(1:1)
	0xFF, 0x02,	0x1F, 0x20,	// V scaling(1:1),H scaling(1:1)
	0xFF, 0x03,	0x1F, 0x20,	// V scaling(1:1),H scaling(1:1)

//---------------------------------------------------------
//	Downscale Cropping Setting ->default
//	Page,	H/V S/E-line[9:8],	V0 S-line[7:0],	V0 E-line[7:0],	V1 S-line[7:0],	V1 E-line[7:0],
	0xFF, 0x00,	0x40, 0xA2,	0x41, 0x16,	0x42, 0x05,	0x43, 0x16,	0x44, 0x05,	0x45, 0x68,
	0xFF, 0x01,	0x40, 0xA2,	0x41, 0x16,	0x42, 0x05,	0x43, 0x16,	0x44, 0x05,	0x45, 0x68,
	0xFF, 0x02,	0x40, 0xA2,	0x41, 0x16,	0x42, 0x05,	0x43, 0x16,	0x44, 0x05,	0x45, 0x68,
	0xFF, 0x03,	0x40, 0xA2,	0x41, 0x16,	0x42, 0x05,	0x43, 0x16,	0x44, 0x05,	0x45, 0x68,

//---------------------------------------------------------
//	Advance Setting
//	Page,                                                                                           Chrome,                         Blus Scree E/D, D-AGC-L,        D-AGC-B,        Notch control,  Bessel,         SYNC_s          A-AGC,                          Blank_R,
	0xFF, 0x00,	0x06, 0x88,	0x09, 0x80,	0x0A, 0xC0,	0x0B, 0x80,	0x0C, 0x80,	0x0E, 0xB0,	0x11,0x03,      0x1A, 0x03,	0x1c, 0xE6,     0x1d, 0xCF,     0x1E, 0x2a,	0x2B, 0x49,	0x2c, 0xfd,	0x32, 0xd0,     0x36, 0x30,     0x38, 0x87,
	0xFF, 0x01,	0x06, 0x88,	0x09, 0x80,	0x0A, 0xC0,	0x0B, 0x80,	0x0C, 0x80,	0x0E, 0xB0,	0x11,0x03,      0x1A, 0x03,	0x1c, 0xE6,     0x1d, 0xCF,     0x1E, 0x2a,	0x2B, 0x49,	0x2c, 0xfd,	0x32, 0xd0,     0x36, 0x30,     0x38, 0x87,
	0xFF, 0x02,	0x06, 0x88,	0x09, 0x80,	0x0A, 0xC0,	0x0B, 0x80,	0x0C, 0x80,	0x0E, 0xB0,	0x11,0x03,      0x1A, 0x03,	0x1c, 0xE6,     0x1d, 0xCF,     0x1E, 0x2a,	0x2B, 0x49,	0x2c, 0xfd,	0x32, 0xd0,     0x36, 0x30,     0x38, 0x87,
	0xFF, 0x03,	0x06, 0x88,	0x09, 0x80,	0x0A, 0xC0,	0x0B, 0x80,	0x0C, 0x80,	0x0E, 0xB0,	0x11,0x03,      0x1A, 0x03,	0x1c, 0xE6,     0x1d, 0xCF,     0x1E, 0x2a,	0x2B, 0x49,	0x2c, 0xfd,	0x32, 0xd0,     0x36, 0x30,     0x38, 0x87,

//---------------------------------------------------------
//	ID Insertion Setting
//	Page,      Chid format,            Chid,
	0xFF, 0x00,	0x3A, 0x20,	0x3E, 0xFF,	0x3F, 0x10,	// Insert Channel ID in SAV/EAV, Channel ID "MSB of XY=1"
	0xFF, 0x01,	0x3A, 0x20,	0x3E, 0xFF,	0x3F, 0x11,	// Insert Channel ID in SAV/EAV, Channel ID "MSB of XY=1"
	0xFF, 0x02,	0x3A, 0x20,	0x3E, 0xFF,	0x3F, 0x12,	// Insert Channel ID in SAV/EAV, Channel ID "MSB of XY=1"
	0xFF, 0x03,	0x3A, 0x20,	0x3E, 0xFF,	0x3F, 0x13,	// Insert Channel ID in SAV/EAV, Channel ID "MSB of XY=1"
};

// ----------------------------------------------------------------------
// External Variable
// ----------------------------------------------------------------------

// ----------------------------------------------------------------------
// Static Prototype Functions
// ----------------------------------------------------------------------

// ----------------------------------------------------------------------
// Static functions
// ----------------------------------------------------------------------

// ----------------------------------------------------------------------
// Exported functions
// ----------------------------------------------------------------------




//--------------------------------------------------------------------------------------------------------------------------
int RN6264_SetVideoSource(u8 mode)
{

	u16 i;

	if (mode == VDCNV_4CH_ON_PAL)
	{
		printf("RN6264 PAL regist \n");
		for(i=0; i<sizeof(defRN6266Sys_PAL)/2; i++)
		{
//			if (defRN6266Sys_PAL[i*2] ==0xff)
//				printf ("select page\n");
			if( RN6264_RegWrite(defRN6266Sys_PAL[i*2], defRN6266Sys_PAL[i*2+1]) < 0)
				return -1;
		}
	}
	else
	if (mode == VDCNV_4CH_ON_NTSC_960)
	{
		printf("RN6264 NTSC_960 regist \n");
		for(i=0; i<sizeof(defRN6266Sys_NTSC_960)/2; i++)
		{
//			if (defRN6266Sys_PAL[i*2] ==0xff)
//				printf ("select page\n");
			if( RN6264_RegWrite(defRN6266Sys_NTSC_960[i*2], defRN6266Sys_NTSC_960[i*2+1]) < 0)
				return -1;
		}
	}
	else
	{
		printf("RN6264 NTSC regist \n");
		for(i=0; i<sizeof(defRN6266Sys_NTSC)/2; i++)
		{
//			if (defRN6266Sys[i*2] ==0xff)
//				printf ("select page\n");
			if( RN6264_RegWrite(defRN6266Sys_NTSC[i*2], defRN6266Sys_NTSC[i*2+1]) < 0)
				return -1;
		}
	}
	return 0;
}



